package com.itheima.tilas.controller;

import com.itheima.tilas.Utils.ResultUtil;
import com.itheima.tilas.annotation.Log;
import com.itheima.tilas.dto.SelectAllDeptDTO;
import com.itheima.tilas.dto.UpdateByIdDTO;
import com.itheima.tilas.entity.Dept;
import com.itheima.tilas.mapper.DeptMapper;
import com.itheima.tilas.po.InsertDeptParam;
import com.itheima.tilas.service.DeptService;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;

/**
 * @RequestMapping Target ->类上,方法上
 *                      类上 ->提取一个controller中的重复路径
 *                      方法上 -> 定义该接口的请求方法的 被对应的GetMapping..PostMapping 替代了
 */
@Slf4j
@RestController
@RequestMapping("/depts")
@CrossOrigin
public class DeptController {

    @Resource
    DeptService deptService;
    @Autowired
    private LogController logController;


    @GetMapping
    public ResultUtil selectAll() {
//        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy年MM月dd日 HH:mm:ss");
        List<SelectAllDeptDTO> depts = deptService.selectAll();
//        List<SelectAllDeptDTO> list = depts.stream().map(o -> {
//            SelectAllDeptDTO dto = new SelectAllDeptDTO();
//            String format = o.getUpdateTime().format(formatter);
//            dto.setId(o.getId());
//            dto.setName(o.getName());
//            dto.setUpdateTime(format);
//            return dto;
//        }).toList();
        return ResultUtil.success(depts);

    }

    /***
     * get/delete 请求参数一般拼接到url后面
     * post/put 请求参数一般放在请求体里面,且以JOSN格式进行传输
     * @RequestBody 作用1:就是从http请求体中读取传输的数据
     *              作用2:把JSON字符串变成对象
     * @return
     */
    @PostMapping
    @Log
    public ResultUtil insertDept(@RequestBody InsertDeptParam param) {
        //把JSON字符串变成对象
        log.info("读取请求体{}", param.getName());
        int flag = deptService.insertDept(param);
        return flag == 0 ? ResultUtil.fail("新增失败") : ResultUtil.success("新增成功");
    }
//    @PostMapping("/depts")
//    public String hello(HttpServletRequest request) {
//
//        try {
//            BufferedReader reader = request.getReader();
//            System.out.println(reader.readLine());
//        } catch (IOException e) {
//            throw new RuntimeException(e);
//        }
//
//        return "";
//    }

    /**
     * @param id
     * @return
     * @RequesParam 注解的作用:
     * 1.根据指定的K,获取Http URL的值
     * 2. 可以指定是否必传参数,如果指定必传 ,前端没传相应参数->报400 required
     * 3. 给参数指定默认值 defaultValue 默认值属性
     */
    @DeleteMapping
    @Log
    public ResultUtil deleteById(@RequestParam("id") Integer id) {
        int flag = deptService.deleteById(id);
        return flag == 0 ? ResultUtil.fail("新增失败") : ResultUtil.success("新增成功");

    }

    /**
     * @param id
     * @return
     * @PathVariable 注解作用
     * 获取URI以/分割的路径变量
     */
    @GetMapping("/{id}")
    public ResultUtil selectById(@PathVariable("id") Integer id) {
        Dept dept = deptService.selectById(id);
        if (Objects.isNull(dept)) {
            return ResultUtil.fail(dept);
        } else {
            return ResultUtil.success(dept);
        }
    }

//    @PutMapping
//    public ResultUtil updateById(@RequestBody UpdateByIdDTO info) {
//        int flag = deptService.updateById(info);
//        return flag == 0 ? ResultUtil.fail("修改失败") : ResultUtil.success(info);
//
//    }

    @PutMapping
    @Log
    public ResultUtil updateById(@RequestBody UpdateByIdDTO info) {
        int flag1 = deptService.selectByIdAndName(info.getId(), info.getName());
        if (flag1 == 0) {
            return ResultUtil.fail("修改失败");
        }
        int flag = deptService.updateById(info);
        return flag == 0 ? ResultUtil.fail("修改失败") : ResultUtil.success(info);

    }

    @DeleteMapping("/{ids}")
    @Log
    public ResultUtil deleteBatchByIds(@PathVariable String ids) {
        List<Integer> list = Arrays.stream(ids.split(",")).map(Integer::valueOf).toList();
        log.info("{}",list);
        int flag = deptService.deleteBatchByIds(list);
        return flag > 0 ? ResultUtil.success("删除了"+flag+"条信息") : ResultUtil.fail("批量删除失败");

    }

    @PostMapping("/bach")
    @Log
    public ResultUtil insertBatch(@RequestBody List<Dept> depts){
        int flag = deptService.insertBatch(depts);
        return flag > 0 ? ResultUtil.success(flag) : ResultUtil.fail("批量插入失败");
    }
}
